package com.whfc.ms.api.emp.service.impl;

import com.whfc.base.service.WarnConfigService;
import com.whfc.common.entity.DefaultProperties;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.util.QRCodeUtil;
import com.whfc.emp.dto.AppEmpSettingDTO;
import com.whfc.emp.enums.AttendType;
import com.whfc.emp.param.AppEmpSettingAddParam;
import com.whfc.emp.service.AppEmpConfigService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.ms.api.emp.service.MsAppEmpConfigService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 人员配置项服务
 *
 * @author hw
 * @version 1.0
 * @date 2020/12/28 18:53
 */
@Service
public class MsAppEmpConfigServiceImpl implements MsAppEmpConfigService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String empQrPath = "emp/qr";

    @DubboReference(interfaceClass = AppEmpConfigService.class, version = "1.0.0", timeout = 5000)
    private AppEmpConfigService appEmpConfigService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0", timeout = 5000)
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = WarnConfigService.class, version = "1.0.0")
    private WarnConfigService warnConfigService;

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private DefaultProperties defaultProps;

    @Override
    public AppEmpSettingDTO getSetting(Integer deptId) {
        return appEmpConfigService.getSetting(deptId);
    }

    @Override
    public void settingAdd(AppEmpSettingAddParam request) {
        appEmpConfigService.settingAdd(request);
    }

    @Override
    public AttendType getAttendType(Integer deptId) {
        return appEmpConfigService.getAttendType(deptId);
    }

    @Override
    public void setAttendType(Integer deptId, AttendType attendType) {
        appEmpConfigService.setAttendType(deptId, attendType);
    }

    @Override
    public boolean isIdCardVerify(Integer deptId) {
        return appEmpConfigService.isIdCardVerify(deptId);
    }

    @Override
    public OssPathDTO downloadAttendQr(Integer deptId) {
        String fileName = "attendqr" + deptId + ".jpg";
        String content = defaultProps.getEmpAttendQrUrl() + deptId;
        String bottomDes = sysDeptService.getDeptNameById(deptId);
        String qr = generateQr(fileName, content, bottomDes);
        appEmpConfigService.saveQr(deptId, qr, "attend_qr");
        return new OssPathDTO(qr);
    }

    @Override
    public OssPathDTO downloadEmpQr(Integer deptId) {
        String fileName = "empqr" + deptId + ".jpg";
        String content = defaultProps.getEmpQrUrl() + deptId;
        String bottomDes = sysDeptService.getDeptNameById(deptId);
        String qr = generateQr(fileName, content, bottomDes);
        appEmpConfigService.saveQr(deptId, qr, "emp_qr");
        return new OssPathDTO(qr);
    }

    @Override
    public boolean checkAttendQr(Integer deptId) {
        return appEmpConfigService.checkQr(deptId, "attend_qr");
    }

    @Override
    public boolean checkEmpQr(Integer deptId) {
        return appEmpConfigService.checkQr(deptId, "emp_qr");
    }

    /**
     * 生成二维码
     *
     * @param fileName  文件名
     * @param content   二维码内容
     * @param bottomDes 二维码底部描述
     * @return 二维码
     */
    private String generateQr(String fileName, String content, String bottomDes) {

        //验证文件是否存在
        String ossKey = filePathConfig.getFilePath(empQrPath, fileName);
        boolean exists = fileHandler.checkFile(ossKey);
        if (exists) {
            return fileHandler.parseUrl(ossKey);
        }

        String path = "";
        try {
            //生成二维码
            File tempFile = File.createTempFile(fileName, ".jpg");
            QRCodeUtil.encode(content, bottomDes, null, tempFile.getAbsolutePath());
            //上传文件服务器
            FileInputStream inputStream = new FileInputStream(tempFile);
            Map<String, String> headers = new HashMap<>();
            headers.put("cache-control", "no-cache");
            path = fileHandler.upload(ossKey, inputStream, headers);
        } catch (Exception e) {
            logger.error("生成考勤二维码出错,error:{}", e.getMessage());
        }
        return path;
    }
}
